<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
      <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="queryList()">搜索</el-button>
      <!-- 新增 -->
      <!-- 新增 -->
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
        <el-button
          type="danger"
          class="filter-item"
          size="mini"
          icon="el-icon-refresh"
          @click="reset()"
        >重置</el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <eAttr ref="form2" :is-attr="isAttr" />
    <editForm ref="form1" :is-add="isAdd" />
    <comForm ref="form3" :is-add="isAdd" />
    <killForm ref="form4" :is-add="isAdd" />
    <bargainForm ref="form5" :is-add="isAdd" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column prop="id" label="商品id" />
      <el-table-column prop="goodsName" label="商品名称"/>
      <el-table-column prop="costPrice" label="供价" />
      <el-table-column prop="sellPrice" label="售价" />
      <el-table-column prop="inventory" label="库存" />
      <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div @click="onSale(scope.row)">
            <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="''">已上架</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" 'info' ">已下架</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="205px" align="center">
        <template slot-scope="scope">
          <el-button slot="reference" type="danger" size="mini" @click="attr(scope.row)">规格属性</el-button>
          <el-dropdown size="mini" split-button type="primary" trigger="click">
            操作
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="success"
                  icon="el-icon-edit"
                  @click="edit(scope.row)"
                >编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-popover
                  :ref="scope.row.id"
                  placement="top"
                  width="180"
                >
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                  </div>
                  <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                </el-popover>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="warning"
                  @click="editD(scope.row)"
                >开启秒杀</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/crud'
import { del, onsale,queryList } from '@/api/StoreProduct'
import eForm from './form'
import eAttr from './attr'
import editForm from './editForm'
import comForm from '@/views/activity/combination/form'
import killForm from '@/views/activity/seckill/form'
import bargainForm from '@/views/activity/bargain/form'
import { parseTime } from '@/utils/index'
import Cookies from 'js-cookie'
export default {
  components: { eForm, eAttr, editForm,comForm, killForm, bargainForm },
  //mixins: [initData],
 data() {
    return {
      // delLoading: false,
      visible: false,
      queryTypeOptions: [
        { key: 'goodsName', display_name: '商品名称' },
        { key: 'id', display_name: '商品编号' }
      ],
      // 表格数据
      data: [],
      isAttr: false,
      page: 0,
      // 每页数据条数
      size: 10,
      // 总数据条数
      total: 0,
      // 查询数据的参数
      params: {},
      // 待查询的对象
      query: {},
      // 等待时间
      time: 50,
      // 是否为新增类型的表单
      isAdd: false,
      // 导出的 Loading
      downloadLoading: false,
      // 表格 Loading 属性
      loading: true,
      // 删除 Loading 属性
      delLoading: false,
      delAllLoading: false,
      // 弹窗属性
      dialog: false,
      // Form 表单
      form: {}
    }
  },
  created() {
    this.$nextTick(() => {
      //this.init()
      this.queryList()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    // beforeInit() {
    //   this.url = 'api/StoreProduct'
    //   const shopId=Cookies.get('shopId')
    //   const params = { page: this.page, size: this.size, shopId:shopId}
    //   const query = this.query
    //   const type = query.type
    //   const value = query.value
    //   if (type && value) { this.params[type] = value }
    //   return true
    // },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.queryList()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    onSale(row) {
      this.$confirm(`确定进行[${!row.status ? '下架' : '上架'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          onsale(row.id, { status: row.status }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.queryList()
              }
            })
          })
        })
        .catch(() => { })
    },
    queryList(){
      const shopId=Cookies.get('shopId')
      const params = { page: this.page, size: this.size, status: 1,shopId:shopId}
     // const query = this.query
      const type = this.query.type
      const value = this.query.value
       if (type && value) { params[type] = value }
     queryList(params).then(res=>{
        this.data=res.data.list
        this.total=res.data.total
          this.loading = false
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
      this.$refs.form.getCates()
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form1
      // _this.getCates()
      _this.form = {
        id: data.id,
        productId:data.productId,
        shopId:data.shopId,
        goodsName: data.goodsName,
        costPrice: data.costPrice,
        sellPrice: data.sellPrice,
        inventory: data.inventory,
        specInfo: data.specInfo,
        createTime: data.createTime,
        createId: data.createId,
        updateTime: data.updateTime,
        updateId: data.updateId,
        isDelete: data.isDelete,
        status: data.status
      }
      _this.dialog = true
    },
    editC(data) {
      this.isAdd = false
      const _this = this.$refs.form3
      _this.form = {
        id: data.id,
        productId:data.productId,
        shopId:data.shopId,
        goodsName: data.goodsName,
        costPrice: data.costPrice,
        sellPrice: data.sellPrice,
        inventory: data.inventory,
        specInfo: data.specInfo,
        createTime: data.createTime,
        createId: data.createId,
        updateTime: data.updateTime,
        updateId: data.updateId,
        isDelete: data.isDelete,
        status: data.status
      }
      _this.dialog = true
    },
    editD(data) {
      this.isAdd = false
      const _this = this.$refs.form4
      _this.form = {
       title: data.goodsName,
       productId:data.id
      }
      _this.dialog = true
    },
    editE(data) {
      this.isAdd = false
      const _this = this.$refs.form5
      _this.form = {
        id: data.id,
        productId:data.productId,
        shopId:data.shopId,
        goodsName: data.goodsName,
        costPrice: data.costPrice,
        sellPrice: data.sellPrice,
        inventory: data.inventory,
        specInfo: data.specInfo,
        createTime: data.createTime,
        createId: data.createId,
        updateTime: data.updateTime,
        updateId: data.updateId,
        isDelete: data.isDelete,
        status: data.status
      }
      _this.dialog = true
    },
    attr(data) {
      this.isAttr = false
      const _this = this.$refs.form2
      _this.form = {
        id: data.id,
        productId:data.productId,
        shopId:data.shopId,
        goodsName: data.goodsName,
        costPrice: data.costPrice,
        sellPrice: data.sellPrice,
        inventory: data.inventory,
        specInfo: data.specInfo,
        createTime: data.createTime,
        createId: data.createId,
        updateTime: data.updateTime,
        updateId: data.updateId,
        isDelete: data.isDelete,
        status: data.status
      }
      _this.dialog = true
      this.$refs.form2.getAttrs(data.id)
    },
    reset(){
      this.query.type='';
      this.query.value='';
    },
    getQueryParame: function() {
      return {
        page: this.page,
        size: this.size,
        sort: this.sort,
        ...this.query,
        ...this.params
      }
    },
    // 改变页码
    pageChange(e) {
      this.page = e - 1
       this.queryList()
    },
    // 改变每页显示数
    sizeChange(e) {
      this.page = 0
      this.size = e
      this.queryList()
    },
    // 预防删除第二页最后一条数据时，或者多选删除第二页的数据时，页码错误导致请求无数据
    dleChangePage(size) {
      if (size === undefined) {
        size = 1
      }
      if (this.data.length === size && this.page !== 0) {
        this.page = this.page - 1
      }
    }
  }
}
</script>

<style scoped>

</style>
